//
//  LGSportsEvaDetailSelectCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/11.
//

#import "LGSportsEvaDetailSelectCell.h"

#import "LGRecoverySectionView.h"

#define itemHeight  viewPix(48)
@interface LGSportsEvaDetailSelectCell()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)LGRecoverySectionView *titleView;
/**   */
@property (nonatomic , strong)NSMutableArray *itemArray;

@end

@implementation LGSportsEvaDetailSelectCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.clipsToBounds = YES;
        self.contentView.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
        [self.contentView addSubview:self.baseView];
        [self.baseView addSubview:self.titleView];
        [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self.contentView);
            make.left.equalTo(self.contentView).offset(viewPix(16));
            make.right.equalTo(self.contentView).offset(-viewPix(16));
            make.height.equalTo(@(itemHeight*2));
        }];
    }
    return  self;
}


-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.titleView.title = LGNSString(dataDic[@"group_name"]);
    
    for (UIView *view in self.itemArray) {
        [view removeFromSuperview];
    }
    [self.itemArray removeAllObjects];
    
    NSArray *questionArray = dataDic[@"questions"];
    CGFloat topMargin = kMaxY(self.titleView.frame);
    for (NSInteger i=0; i<questionArray.count; i++) {
        NSDictionary *questionDic = questionArray[i];
        if(LGNSString(questionDic[@"title"]).length>0){
            [self createSubtileWithTop:topMargin title:LGNSString(questionDic[@"title"])];
            topMargin += viewPix(34);
        }
        NSArray *itemArray = questionDic[@"items"];
        for (NSInteger j=0; j<itemArray.count; j++) {
            NSDictionary *itemDic = itemArray[j];
            [self itemViewWithTop:topMargin title:LGNSString(itemDic[@"answer"]) itemArray:itemDic[@"options"]];
            topMargin += itemHeight;
        }
        
        UIButton *touchButton = [UIButton buttonWithType:UIButtonTypeSystem];
        touchButton.frame = CGRectMake(0, topMargin-itemHeight-viewPix(34), Screen_W-viewPix(32), itemHeight+viewPix(34));
        [touchButton setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
        [touchButton setTitle:LGNSString(questionDic[@"name"]) forState:UIControlStateNormal];
        [touchButton addTarget:self action:@selector(itemButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
        [self.baseView addSubview:touchButton];
        [self.itemArray addObject:touchButton];
    }
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(12), topMargin, Screen_W-viewPix(56), 0.5)];
    lineView.backgroundColor = [UIColor colorWithString:@"#E0E0E0"];
    [self.baseView addSubview:lineView];
    [self.itemArray addObject:lineView];
    
    [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(topMargin+2));
    }];
}

//查看趋势图
-(void)itemButtonTouched:(UIButton *)sender{
//    if(self.delegate && [self.delegate respondsToSelector:@selector(checkRecordChart:)]){
//        [self.delegate checkRecordChart:sender.titleLabel.text];
//    }
}

#pragma mark
#pragma mark ====> 创建控件
-(NSMutableArray *)itemArray{
    if(!_itemArray){
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}
-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:0];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(LGRecoverySectionView *)titleView{
    if(!_titleView){
        _titleView = [[LGRecoverySectionView alloc]initWithFrame:CGRectMake(0, viewPix(3), Screen_W-viewPix(32), viewPix(40))];
    }
    return _titleView;
}


-(void)createSubtileWithTop:(CGFloat)topMargin title:(NSString *)title{
    UILabel *label = [UILabel lableWithText:title colorString:@"#333333" textFont:LGFont(13) textAlignment:NSTextAlignmentLeft lines:1];
    label.frame = CGRectMake(viewPix(16), topMargin, Screen_W-viewPix(64), viewPix(30));
    [self.baseView addSubview:label];
    [self.itemArray addObject:label];
}

-(void)itemViewWithTop:(CGFloat)topMargin title:(NSString *)title itemArray:(NSArray *)itemArray{
    NSInteger lineCount = (itemArray.count>4)?4:itemArray.count;
    CGFloat itemWidth = (Screen_W-viewPix(16)-viewPix(32))/lineCount;
    for (NSInteger i=0; i<itemArray.count; i++) {
        UIButton *button = [UIButton buttonWithTitle:LGNSString(itemArray[i]) titleFont:LGFontWeight(15, UIFontWeightMedium) normalColor:@"#666666" selectColor:@"#FFFFFF"];
        button.frame = CGRectMake(viewPix(12)+itemWidth*(i%lineCount), topMargin+itemHeight*(i/lineCount), itemWidth-viewPix(8), itemHeight-viewPix(16));
        button.cornerRidus = viewPix(16);
        button.selected = [LGNSString(itemArray[i]) isEqualToString:title]?YES:NO;
        [button setBackgroundColor:[UIColor colorWithString:@"#F6F6F6"] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor colorWithString:@"#3291FF"] forState:UIControlStateSelected];
        button.userInteractionEnabled = NO;
        [self.baseView addSubview:button];
        [self.itemArray addObject:button];
    }
}

@end
